<template>
  <view>

    <!-- 轮播图 -->
    <swiper class="goods-detail" :indicator-dots="true" :autoplay="true" :interval="3000" :duration="1000">
      <swiper-item class="goods-detail-swiper-item" @click="previewImg(index)" v-for="(item,index) in goods_detail.pics"
        :key="index">
        <image class="goods-detail-image" :src="item.pics_big"></image>
      </swiper-item>
    </swiper>
    <!-- 商品信息区域 -->
    <view class="goods-detail-info">
      <!-- 价格区域 -->
      <view class="price">
        ${{goods_detail.goods_price}}
      </view>
      <!-- 文字区域 -->
      <view class="text-box">
        <view class="text">
          {{goods_detail.goods_name}}
        </view>
        <view class="favor">
          <text>收藏</text>
          <uni-icons type="star" size="20" color="gray"></uni-icons>
        </view>
      </view>
      <!-- 快递 -->
      <view class="tans">
        快递:免运费
      </view>
    </view>
    <!-- 商品图文介绍区域 -->
    <view class="goods-infoimg-box">
      <!-- 底部导航 -->
      <rich-text :nodes="goods_detail.goods_introduce"></rich-text>
    </view>
    <uni-goods-nav class="goods-nav-box" :fill="true" :options="options" :buttonGroup="buttonGroup" @click="onClick"
      @buttonClick="buttonClick" />
  </view>
</template>

<script>
  import {
    mapState,
    mapMutations,
    mapGetters
  } from 'Vuex'

  export default {

    computed: {
      ...mapState('m_cart', ['cart']),
      ...mapGetters('m_cart', ['totalCheckCount'])
    },
    watch: {
      totalCheckCount: {
        handler(newVal) {
        
          const res = this.options.find(item => item.text === '购物车')
          if (res) {
            res.info = newVal
          }
        },
          immediate: true
      }
    },
    data() {
      return {
        goods_detail: {},
        goods_id: 0,
        options: [{
          icon: 'shop',
          text: '店铺',
          info: 0,
          infoBackgroundColor: '#ff0000',
          infoColor: "fff"
        }, {
          icon: 'cart',
          text: '购物车',
          info: 0
        }],
        buttonGroup: [{
            text: '加入购物车',
            backgroundColor: '#ff0000',
            color: '#fff'
          },
          {
            text: '立即购买',
            backgroundColor: '#ffa200',
            color: '#fff'
          }
        ]

      }
    },
    onLoad(options) {
      this.goods_id = options.goods_id
      this.getGoodsDetaill(this.goods_id)
    },
    methods: {
      ...mapMutations('m_cart', ['addToCart']),
      async getGoodsDetaill(id) {
        const {
          data: res
        } = await uni.$http.get(`/api/public/v1/goods/detail`, {
          goods_id: id
        })
        if (res.meta.status !== 200) {
          return uni.$showMsg()
        }
        res.message.goods_introduce = res.message.goods_introduce.replace(/.webp/g, '.jpg').replace(/<img/g,
          '<img style:"display:block;"')
        this.goods_detail = res.message
      },
      previewImg(index) {
        uni.previewImage({
          current: index,
          urls: this.goods_detail.pics.map(img => img.pics_big)
        })
      },
      onClick(e) {
        if (e.content.text === '购物车') {
          uni.switchTab({
            url: '/pages/cart/cart'
          })
        }
        if (e.content.text === '店铺') {
          uni.$showMsg('店铺')
        }

      },
      buttonClick(e) {

        if (e.content.text === '加入购物车') {
          const goods = {
            goods_id: this.goods_detail.goods_id,
            goods_name: this.goods_detail.goods_name,
            goods_price: this.goods_detail.goods_price,
            goods_small_logo: this.goods_detail.goods_small_logo,
            goods_count: 1,
            goods_state: true
          }
          this.addToCart(goods)
        }
        if (e.content.text === '立即购买') {}

      }
    }
  }
</script>

<style lang="scss">
  .goods-detail {
    height: 750rpx;

    image {
      height: 100%;
      width: 100%;
    }
  }

  .goods-detail-info {
    .price {
      color: red;
      font-size: 20px;
      padding: 10px 5px;
    }

    .text-box {
      display: flex;
      justify-content: space-between;

      .text {
        font-size: 12px;
        margin: 10px 5px;

      }

      .favor {
        margin-right: 10px;
        width: 200rpx;
        font-size: 16px;
        display: flex;
        flex-direction: column;
        justify-content: center;
        align-items: center;
        border-left: 2px solid #efefef;
      }
    }
  }

  .goods-infoimg-box {
    padding-bottom: 50px;
  }

  .goods-nav-box {
    position: fixed;
    z-index: 999;
    bottom: 0;
    left: 0;
    width: 100%;
  }
</style>